//
//  LZReviewTableViewCellReplyView.m
//  laziz_Merchant
//
//  Created by xbn_iMac on 2017/3/1.
//  Copyright © 2017年 XBN. All rights reserved.
//

#import "LZReviewTableViewCellReplyView.h"
#import "LZReviewsReplyModel.h"
#import "LZDateFormatterManager.h"
#import "Masonry.h"

@interface LZReviewTableViewCellReplyAndDeleteButton ()

@property (nonatomic, strong) UIImageView *imgView;
@property (nonatomic, strong) UILabel *lab;

@end

@implementation LZReviewTableViewCellReplyAndDeleteButton

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.layer.cornerRadius = CGRectGetHeight(frame)/2.0;
        [self configSubviews];
    }
    return self;
}

- (void)configSubviews {
    self.imgView = [[UIImageView alloc] init];
    [self addSubview:self.imgView];
    
    self.lab = [[UILabel alloc] init];
    self.lab.font = [UIFont systemFontOfSize:12];
    self.lab.textColor = [UIColor whiteColor];
    [self addSubview:self.lab];
    
    [self.imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(12.5);
        make.centerY.equalTo(self);
    }];
    
    [self.lab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.imgView.mas_trailing).offset(5);
        make.centerY.equalTo(self);
    }];
}

- (void)setImage:(UIImage*)aImage andLabelText:(NSString*)aString {
    self.imgView.image = aImage;
    self.lab.text = aString;
}

- (CGSize)intrinsicContentSize {
    return CGSizeMake(self.imgView.intrinsicContentSize.width+5+self.lab.intrinsicContentSize.width+25, CGRectGetHeight(self.frame));
}

@end

@interface LZReviewTableViewCellReplyView ()

@property (nonatomic, strong) UIImageView *replyImageView;

@property (nonatomic, strong) UILabel *labReplyTitle;
@property (nonatomic, strong) UILabel *labReplyTime;
@property (nonatomic, strong) UIView *line;
@property (nonatomic, strong) UILabel *labReplyContent;
@property (nonatomic, strong) LZReviewTableViewCellReplyAndDeleteButton *replyButton;

@end

@implementation LZReviewTableViewCellReplyView

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.backgroundColor = [UIColor clearColor];
    
    [self configSubviews];
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
}

- (void)setModel:(LZReviewsReplyModel *)model {
    _model = model;
    
    self.replyImageView.hidden = (model == nil);
    
    self.labReplyContent.text = model.replies;
    
    self.labReplyTime.text = [[LZDateFormatterManager shareInstance] stringFromStringNormalHaveLineyyyyMMdd:model.replyTime];
    
    if (model) {
        [self.replyButton setImage:[UIImage imageNamed:@"reviews_reply_button_del"] andLabelText:LZLocalizedString(@"review_delete", @"")];
        [self.replyButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(self.replyButton.intrinsicContentSize.width);
        }];
    }
    else {
        [self.replyButton setImage:[UIImage imageNamed:@"reviews_reply_button_reply"] andLabelText:LZLocalizedString(@"review_reply", @"")];
        [self.replyButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(self.replyButton.intrinsicContentSize.width);
        }];
    }
}

- (void)configSubviews {
    self.replyImageView = [[UIImageView alloc] init];
    [self addSubview:self.replyImageView];
    
    UIImage *image = ([LZLanguageManager shareInstance].getLanguageType == LanguageType_Ar) ? [UIImage imageNamed:@"reviews_reply_text_bg_ar"] : [UIImage imageNamed:@"reviews_reply_text_bg_en"];
    image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(20, 20, 20, 20)];
    self.replyImageView.image = image;
    
    self.labReplyTitle = [[UILabel alloc] init];
    self.labReplyTitle.textColor = UIColorFromRGB(0x666666, 1);
    self.labReplyTitle.text = LZLocalizedString(@"review_reply", @"");
    self.labReplyTitle.font = [UIFont systemFontOfSize:13];
    [self.replyImageView addSubview:self.labReplyTitle];
    
    
    self.labReplyTime = [[UILabel alloc] init];
    self.labReplyTime.textColor = UIColorFromRGB(0x999999, 1);
    self.labReplyTime.font = [UIFont systemFontOfSize:11];
    [self.replyImageView addSubview:self.labReplyTime];
    
    
    self.line = [[UIView alloc] init];
    self.line.backgroundColor = UIColorFromRGB(0xffffff, 1);
    [self.replyImageView addSubview:self.line];
    
    
    self.labReplyContent = [[UILabel alloc] init];
    self.labReplyContent.textColor = UIColorFromRGB(0x666666, 1);
    self.labReplyContent.font = [UIFont systemFontOfSize:13];
    self.labReplyContent.numberOfLines = 0;
    self.labReplyContent.preferredMaxLayoutWidth = SCREEN_WIDTH-89;
    [self.replyImageView addSubview:self.labReplyContent];
    
    self.replyButton = [[LZReviewTableViewCellReplyAndDeleteButton alloc] initWithFrame:CGRectMake(0, 0, 0, 18)];
    self.replyButton.backgroundColor = UIColorFromRGB(0x33a928, 1);
    [self.replyButton addTarget:self action:@selector(clickReplyButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.replyButton];
    
    [self configVFL];
    
    [self.replyButton setImage:[UIImage imageNamed:@"reviews_reply_button_reply"] andLabelText:LZLocalizedString(@"review_reply", @"")];
    [self.replyButton mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.replyButton.intrinsicContentSize.width);
    }];
}

- (void)configVFL {
    [self.replyImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.leading.and.trailing.equalTo(0);
        make.bottom.equalTo(self.labReplyContent.mas_bottom).offset(12.5);
    }];
    
    [self.labReplyTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(10);
        make.top.equalTo(12.5);
    }];
    
    [self.labReplyTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(-10);
        make.centerY.equalTo(self.labReplyTitle);
    }];
    
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(10);
        make.trailing.equalTo(-10);
        make.top.equalTo(self.labReplyTitle.mas_bottom).offset(10);
        make.height.equalTo(0.5);
    }];
    
    [self.labReplyContent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(5+5);
        make.top.equalTo(self.line.mas_bottom).offset(10);
        make.trailing.equalTo(-5);
    }];
    
    [self.replyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.and.bottom.equalTo(0);
        make.height.equalTo(18);
        make.width.equalTo(100);
    }];
}

- (void)clickReplyButtonAction:(LZReviewTableViewCellReplyAndDeleteButton*)sender {
    if (self.clickButtonBlock) {
        self.clickButtonBlock((self.model == nil)?ReviewsButtonType_Reply:ReviewsButtonType_Delete);
    }
}

- (CGSize)intrinsicContentSize {
    if (self.model) {
         return CGSizeMake(0, 12.5 + self.labReplyTitle.intrinsicContentSize.height + 10 + 0.5 + 10 +  self.labReplyContent.intrinsicContentSize.height + 12.5 + 20 + self.replyButton.intrinsicContentSize.height);
    }
    else {
        return CGSizeMake(0, self.replyButton.intrinsicContentSize.height);
    }
}

@end
